﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JeanFish.Blog.Storage.Providers.EF;
using System.Data.Entity;
using JeanFish.Blog.Business;

namespace JeanFish.Blog.Storage.Providers
{
    public class EFDataProvider : BlogDataProvider
    {

        public async override Task<IEnumerable<Post>> ListPostsAsync(Blog blog, Paging paging)
        {
            using (var db = new EF.BlogEntities())
            {
                var posts = db.Post.HasNoPostLabel(PostLabel.Deleted);
                paging.Total =await posts.CountAsync();
                if (paging.Total > 0)
                {

                    return await posts
                        .OrderByDescending(t => t.created)
                        .Skip(paging.ActualIndex * paging.Size)
                        .Take(paging.Size)
                        .Select(p => new Post()
                        {
                            ID = p.id,
                            Blog = p.blog,
                            Title = p.title,
                            Text = p.text,
                            CreatedTime = p.created,
                            ModifiedTime = p.modified,
                            Author = p.author,
                            Labels = p.label
                        }).ToListAsync();
                }
                else
                {
                    return new List<Post>();
                }
            }
        }

        public async override Task<SavedResult> SaveBlogAsync(Blog blog)
        {
            return await Task.FromResult(SavedResult.OK);
        }

        public async override Task<SavedResult> CreatePostAsync(Post post)
        {
            using (var db = new EF.BlogEntities())
            {
                if (await db.Post.AnyAsync(t => t.id == post.ID)) return SavedResult.Oops;
                db.Post.Add(new EF.Post()
                {
                    id = post.ID,
                    blog = post.Blog,
                    title = post.Title,
                    author = post.Author,
                    label = post.Labels,
                    text = post.Text,
                    created = post.CreatedTime,
                    modified = post.ModifiedTime
                });
                return await db.SaveChangesAsync() == 1 ? SavedResult.OK : SavedResult.Oops;
            }
        }

        public async override Task<SavedResult> UpdatePostAsync(Post post)
        {
            using (var db = new EF.BlogEntities())
            {
                if (!await db.Post.AnyAsync(t => t.id == post.ID)) return SavedResult.Oops;
                db.Entry<EF.Post>(new EF.Post()
                {
                    id = post.ID,
                    blog = post.Blog,
                    title = post.Title,
                    author = post.Author,
                    label = post.Labels,
                    text = post.Text,
                    created = post.CreatedTime,
                    modified = post.ModifiedTime
                }).State = EntityState.Modified;
                return await db.SaveChangesAsync() == 1 ? SavedResult.OK : SavedResult.Oops;
            }
        }

        public async override Task<Post> GetPostAsync(Blog blog, string postId)
        {
            using (var db = new EF.BlogEntities())
            {
                var posts = db.Post.HasNoPostLabel(PostLabel.Deleted);
                var p = await posts.FirstOrDefaultAsync(t => t.id == postId);
                if (p == null) return null;
                return new Post()
                {
                    ID = p.id,
                    Blog = p.blog,
                    Title = p.title,
                    Text = p.text,
                    CreatedTime = p.created,
                    ModifiedTime = p.modified,
                    Author = p.author,
                    Labels = p.label
                };
            }
        }
    }
}
